package com.information.manage.MaintenanceRecord.controller;

import com.information.manage.MaintenanceRecord.pojo.Bczhuyou;
import com.information.manage.MaintenanceRecord.service.BczhuyouService;
import com.information.manage.basic.common.result.Result;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import com.alibaba.excel.EasyExcel;

@RestController
@Slf4j
public class BczhuyouController {

    @Autowired
    private BczhuyouService bczhuyouService;

    @GetMapping("/MaintenanceRecord/bczhuyou/selectAll")
    public Result<List<Bczhuyou>> selectAll() {
        List<Bczhuyou> records = bczhuyouService.selectAll();
        return Result.ok(records);
    }

    @PostMapping("/MaintenanceRecord/bczhuyou/add")
    public Result<String> addBczhuyou(@RequestBody Bczhuyou bczhuyou) {
        try {
            bczhuyouService.addMaintBczhuyou(bczhuyou);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error("添加补偿装置主要信息记录出现异常: ", e);
            return Result.fail("添加失败");
        }
    }

    @PutMapping("/MaintenanceRecord/bczhuyou/update/{bczyid}")
    public Result<String> updateBczhuyou(@PathVariable("bczyid") Integer bczyid, @RequestBody Bczhuyou bczhuyou) {
        try {
            bczhuyou.setBczyid(bczyid);
            bczhuyouService.updateMaintBczhuyou(bczhuyou);
            return Result.ok("更新成功");
        } catch (Exception e) {
            log.error("更新补偿装置主要信息记录出现异常: ", e);
            return Result.fail("更新失败");
        }
    }

    @DeleteMapping("/MaintenanceRecord/bczhuyou/delete/{bczyid}")
    public Result<String> deleteBczhuyou(@PathVariable("bczyid") Integer bczyid) {
        try {
            bczhuyouService.logicDeleteMaintBczhuyou(bczyid);
            return Result.ok("删除成功");
        } catch (Exception e) {
            log.error("逻辑删除补偿装置主要信息记录出现异常: ", e);
            return Result.fail("删除失败");
        }
    }

    @GetMapping("/MaintenanceRecord/bczhuyou/download")
    public void download(HttpServletResponse response) throws IOException {
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());

            String fileName = URLEncoder.encode("补偿装置主要信息记录数据", StandardCharsets.UTF_8.name())
                    .replaceAll("\\+", "%20");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename*=utf-8''" + fileName + ".xlsx");

            List<Bczhuyou> list = bczhuyouService.selectAll();
            log.info("查询后的数据列表: {}", list);
            if (list == null || list.isEmpty()) {
                response.getWriter().write("暂无补偿装置主要信息记录数据可供下载");
                return;
            }
            try {
                EasyExcel.write(response.getOutputStream(), Bczhuyou.class)
                        .sheet("补偿装置主要信息记录数据")
                        .doWrite(list);
            } catch (Exception e) {
                response.getWriter().write("下载过程中数据写入出现错误，请联系管理员");
            }
        } catch (IOException e) {
            response.getWriter().write("系统出现异常，下载失败，请稍后再试");
        }
    }
}